package com.carepad.action.bypass;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;

import com.carepad.action.AbstractManagementAction;
import com.carepad.entity.Users;
import com.carepad.entity.mapper.SelectClient;
import com.carepad.service.impl.UsersServiceImpl;


public class BypassViewAction extends AbstractManagementAction{

	private static final long serialVersionUID = 1L;
	private String hcsid;
	private Users users;
	private int year;
	private int month;
	private int date;
	
	@Autowired
	private UsersServiceImpl usersServiceImpl;
	
	@SuppressWarnings("deprecation")
	public String execute() throws ParseException {
		
		try {
			//Signin Process
			setUsers(usersServiceImpl.signIn("kida", "kida"));
			if (getUsers() == null) {
				addActionError(getText("user.signin.failed"));
				return INPUT;
			}
			addSessionUserVariable(getUsers());
			
			//Set Patient
			SelectClient  patient = getPatientByHCSId(getHcsid());
			addSessionVariable(patient);		
			setCookie("CLIENTUID",patient.getUid().toString());
			
			//Set Implementation Date
			try {
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
				
	        	Date lastdate = sdf.parse("1900/01/01");
	        	Date today = new Date();
	        	Date userdate = new Date(getYear()-1900, getMonth()-1, getDate());
	        	
	        	if(userdate.compareTo(today)>0 || userdate.compareTo(lastdate)<0) {
	        		
	        		addActionError(getText("bypass.action.error.date"));
	    			return INPUT;
	        		
	        	} else {
	        		
	        		setSessionDate(userdate);
	        	}
			} catch (Exception e) {
				addActionError(getText("bypass.action.error.date"));
				return INPUT;
			}			
			
			return SUCCESS;
			
		} catch (Exception e) {
			e.printStackTrace();
			addActionError(getText("bypass.action.error"));
			return INPUT;
		}
	}

	public Users getUsers() {
		return users;
	}

	public void setUsers(Users users) {
		this.users = users;
	}

	public String getHcsid() {
		return hcsid;
	}

	public void setHcsid(String hcsid) {
		this.hcsid = hcsid;
	}

	public int getYear() {
		return year;
	}

	public void setYear(int year) {
		this.year = year;
	}

	public int getMonth() {
		return month;
	}

	public void setMonth(int month) {
		this.month = month;
	}

	public int getDate() {
		return date;
	}

	public void setDate(int date) {
		this.date = date;
	}
}